/* 
 *  Created By: Mike Stevens
 *  Project URL: http://www.codeplex.com/sitesecuritymgmt
 *  License: GNU GPL v2
 *  Copyright (C) 2008 Mike Stevens
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using SiteSecurityManagement.Service;
using System.Collections.Generic;
using SiteSecurityManagement.Entities;
using SiteSecurityManagement.Service.SecurityTree;

namespace SiteSecurityManagement
{
    public partial class SecurityTree : UserControl
    {

        #region Members
        private bool m_showCheckBoxes;
        private bool m_navigationUrl;

        private string m_sessionVarName = "expandedNodes";
        #endregion

        #region Properties
        public bool ShowCheckBoxes
        {
            get { return m_showCheckBoxes; }
            set { m_showCheckBoxes = value; }
        }

        public bool EnableNavigationUrl
        {
            get { return m_navigationUrl; }
            set { m_navigationUrl = value; }
        }
	
        #endregion

        #region Methods
        public TreeNodeCollection GetSelectedItems()
        {
            return trvSecNav.CheckedNodes;
        }

        public char GetPathSeperator()
        {
            return trvSecNav.PathSeparator;
        }        
        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (true == m_showCheckBoxes)
                trvSecNav.ShowCheckBoxes = TreeNodeTypes.All;
            else
                trvSecNav.ShowCheckBoxes = TreeNodeTypes.None;

            Session.Remove("restored");

            //if not a postback fill the nodes and save the expanded state
            if (false == Page.IsPostBack)
            {
                SecurityTreeNodeUtility secTreeNodeUtil = new SecurityTreeNodeUtility(m_navigationUrl,this.GetPathSeperator());
                secTreeNodeUtil.FillTreeNodes(this.trvSecNav);

                if (true == SecurityTreeState.IsStateSaved(m_sessionVarName))
                {
                    SecurityTreeState.RestoreTreeView(trvSecNav, m_sessionVarName);
                    Session["restored"] = true;
                }
            }
        }

        protected void trvSecNav_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
        {
            //if a previously expanded node was collapsed remove it from the expansion restore list
            TreeNode node = e.Node;
            SecurityTreeState.RemoveExpandedNode(node, m_sessionVarName);
        }

        protected void trvSecNav_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            //if expand state was restored don't run expansion logic
            if (Session["restored"] == null)
            {
                //get the current node and save the expansion state
                TreeNode node = e.Node;
                SecurityTreeState.SaveExpandedNode(node, m_sessionVarName);

                //fill the children
                SecurityTreeNodeUtility secTreeNodeUtil = new SecurityTreeNodeUtility(m_navigationUrl, this.GetPathSeperator());
                secTreeNodeUtil.FillChildNodes(node);
            }
        }
        #endregion
    }
}